package org.hxzon.blogbackup.baidu;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.hxzon.util.DebugUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class PageBackuper {

    public static void backupPageInfo(int start, int end) {
        BlogPage page;
        BlogFile item;
        for (int i = start; i <= end; i++) {
            try {

                Document doc = BlogReader.get(BlogWriter.indexBaseUrl + i);
//				DebugUtil.debug("doc=" + doc);
                Element body = doc.body();
                Element blogList = body.getElementById("m_blog");//.select("div#m_blog").get(0);
//				DebugUtil.debug("blog list=" + blogList.html());
                Elements blogTitleList = blogList.select("div.tit");
                page = new BlogPage();
                page.setIndex(i);
                for (Element title : blogTitleList) {
                    Element link = title.child(0);
                    Element date = title.nextElementSibling();
                    Element opt = date.nextElementSibling();
                    Element category = opt.getElementsByAttributeValue("title", "查看该分类中所有文章").first();
                    item = new BlogFile();
                    item.setPageIndex(i);
                    item.setOrigUrl(BlogWriter.baseUrl + link.attr("href"));
                    item.setTitle(link);
//					DebugUtil.debug("title=" + item.getTitle());
                    item.setDate(date);
                    item.setCategory(category);
                    page.addBlogFile(item);
                    ///hxzon/blog/item/fdd0dcb476e1227d8bd4b2a1.html
//					System.out.println(title.child(0).attr("href"));
                }
                BlogWriter.writePageInfo(page);
            } catch (Exception e) {
                DebugUtil.error("fetch err," + i + ":" + e.getMessage(), e);
            }
        }
    }

    public static Map<String, String> fileNameMap = new HashMap<String, String>();

    public static void checkFileExist(int index) {
        try {
            Document doc = Jsoup.parse(BlogWriter.readPageInfo(index));
            Element body = doc.body();
            Elements items = body.getElementsByClass("item");
            for (Element item : items) {
                Element title = item.getElementsByClass("tit").first();
                Element category = item.getElementsByClass("cate").first();
                String url = item.getElementsByClass("url").first().html();
                BlogFile file = new BlogFile();
                file.setOrigUrl(url);
                file.setCategory(category);
                file.setTitle(title);
                String preUrl = fileNameMap.get(file.getFileName());
                if (preUrl != null) {
                    DebugUtil.info("multi," + file.getFileName() + "," + preUrl + ",\n" + file.getOrigUrl());
                }
                fileNameMap.put(file.getFileName(), file.getOrigUrl());
                if (!BlogWriter.isItemExist(file)) {
                    DebugUtil.info("file no exist,index=" + index + "," + file.getCategoryText() + "," + file.getFileName() + "," + file.getOrigUrl());
//					file = FileBackuper.exportItem(url, index);
//					if (file.getContent() == null) {
//						throw new NullPointerException("file content is null,url=" + file.getOrigUrl() + ",title" + file.getTitle().text());
//					}
//					BlogWriter.writeItem(file);
                }
//				throw new RuntimeException("category remove substring for this");
            }
        } catch (Exception e) {
            DebugUtil.error("fetch item err," + index + ":" + e.getMessage(), e);
        }
    }

    public static void backupHxzon() {
        PageBackuper.backupPageInfo(201, 255);
        PageBackuper.backupPageInfo(151, 200);
        PageBackuper.backupPageInfo(101, 150);
        PageBackuper.backupPageInfo(51, 100);
        PageBackuper.backupPageInfo(0, 50);
    }

    public static void test() {
        PageBackuper.backupPageInfo(242, 242);
    }

    public static void checkFileExist() {
        for (int i = 255; i >= 0; i--) {
            PageBackuper.checkFileExist(i);
        }
    }

    public static void main(String args[]) throws IOException {
        DebugUtil.clearDebugClass();
        DebugUtil.addDebugClass(PageBackuper.class.getCanonicalName());
//        checkFileExist();
//        test();
    }
}
